﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.EntityFramework.ModelMapper;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.EntityFramework.Implementation.Dictionary
{
    /// <summary>
    /// AccountDao  class
    /// </summary>
    public class EntityAccountDao : IAccountDao
    {
        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsForComboTree(int accountId)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsByParent(int parentId)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetPaged(int unitId, int currentPage)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var result = context.Accounts.AsQueryable().Where(x => x.UnitID == unitId).OrderBy(x => x.AccountCode).ToList();
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0 && currentPage > 0)
                {
                    var start = (currentPage - 1) * pageSize;
                    result = result.Skip(start).Take(pageSize).OrderBy(x => x.AccountCode).ToList();
                }
                return result.Map().ToList();
            }
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsForIsInventoryItem(int unitId)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsForIsTax(int unitId)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsByUnit(int unitId)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsForIsFixedasset(int unitId)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Account> GetAccountsForIsAccountingObject(int unitId)
        {
            throw new NotImplementedException();
        }

        public int GetPageSize(int unitId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                var totalRow = context.Accounts.Count();
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0)
                {
                    var round = totalRow % pageSize;
                    if (round > 0)
                    {
                        return totalRow / pageSize + 1;
                    }
                    return totalRow / pageSize;

                }
                return 1;
            }
        }

        public BusinessObjects.BussinessDictionary.Account GetAccount(int AccountId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return context.Accounts.SingleOrDefault(x => x.AccountID == AccountId).Map();
            }
        }

        public string InsertAccount(BusinessObjects.BussinessDictionary.Account Account)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    context.Accounts.Add(Account.Map());
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string UpdateAccount(BusinessObjects.BussinessDictionary.Account Account)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.Accounts.SingleOrDefault(x => x.AccountID == Account.AccountId);
                    if (result != null)
                    {
                        result.AccountID = Account.AccountId;
                        result.UnitID = Account.UnitId;
                        result.AccountCode = Account.AccountCode;
                        result.Balanceside = Account.BalanceSide;
                        result.Description = Account.Description;
                        result.Grade = Account.Grade;
                        result.IsActive = Account.IsActive;
                        result.IsCustomer = Account.IsCustomer;
                        result.IsDetail = Account.IsDetail;
                        result.IsFixedasset = Account.IsFixedAsset;
                        result.IsInventoryItem = Account.IsInventoryItem;

                        result.IsSystem = Account.IsSystem;
                        result.IsTax = Account.IsTax;
                        result.IsVendor = Account.IsVendor;
                        result.ParentID = Account.ParentId;
                        result.AccountName = Account.AccountName;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string DeleteAccount(BusinessObjects.BussinessDictionary.Account Account)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.Accounts.SingleOrDefault(x => x.AccountID == Account.AccountId);
                    if (result != null)
                    {
                        context.Accounts.Remove(result);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string InsertAccountByUnit(int unitId)
        {
            throw new NotImplementedException();
        }
    }
}
